package org.test4j.integration.spring.faker;

import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.ApplicationContext;
import org.test4j.mock.Invocation;
import org.test4j.mock.Mock;
import org.test4j.mock.MockUp;

import static org.test4j.integration.spring.SpringStartup.ReferenceAnnotationBeanPostProcessor_Name;

/**
 * mock dubbo ReferenceAnnotationBeanPostProcessor
 *
 * @author darui.wu
 */
public class FakeReferenceAnnotationBeanPostProcessor extends MockUp {
    private transient ApplicationContext context;

    public FakeReferenceAnnotationBeanPostProcessor() {
        super(ReferenceAnnotationBeanPostProcessor_Name);
    }

    @Mock
    public void setApplicationContext(Invocation inv, ApplicationContext applicationContext) {
        this.context = applicationContext;
        inv.proceed();
    }

    @Mock
    public Object doGetInjectedBean(Invocation it, Object reference, Object bean, String beanName, Class injectedType, Object injectedElement) {
        try {
            return context.getBean(injectedType);
        } catch (NoSuchBeanDefinitionException e) {
            return it.proceed();
        }
    }
}